﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ch04
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b;   //定义变量保存两个操作数
            double result = 0; //定义变量保存运算结果
            char op;    //定义变量保存运算符

            Console.Write("请输入一个算术表达式：");
            string exp = Console.ReadLine();
            Regex reg = new Regex("[+*/-]");
            Match m = reg.Match(exp);
            //获取匹配到的运算符的下标
            int index = m.Index;

            op = exp[index];
            a = double.Parse(exp.Substring(0,index));
            b = double.Parse(exp.Substring(index+1));

            /*
            Console.Write("第一个数：");
            a = double.Parse(Console.ReadLine());
            Console.Write("第二个数：");
            b = double.Parse(Console.ReadLine());
            Console.Write("运算符：");
            op = char.Parse(Console.ReadLine());
            */
            
            switch (op)
            {
                case '+':result = a + b;break;
                case '-': result = a - b; break;
                case '*': result = a * b; break;
                case '/': result = a / b; break;
                default: Console.WriteLine("运算符有误！");break;
            }

            Console.WriteLine("{0}{1}{2}={3}", a, op, b, result);
            
        }
    }
}
